{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "999f0b51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 10, 10, 13, 25, 52, 17211)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从python基础库(标准库中)找到datetime模块\n",
    "from datetime import datetime\n",
    "datetime.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3659d44",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课本代码(项目一、从奇数中选择对应时间节点)\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"在odds里面\")\n",
    "else:\n",
    "    print(\"不在odds里面\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "052a0608",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入模块\n",
    "import datetime\n",
    "import time\n",
    "import random\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "# 循环遍历5次\n",
    "for i in range(5):\n",
    "    right_this_minute = datetime.datetime.today().minute\n",
    "    print(\"等待时间为：\",wait_time)\n",
    "    if right_this_minute in odds:\n",
    "        print(\"This minute seems a little odd.\", \"当前分钟数为：\", right_this_minute)\n",
    "        print(\"----------\")\n",
    "    else:\n",
    "        print(\"Not an odd minute.\", \"当前分钟数为：\", right_this_minute )\n",
    "        print(\"----------\")\n",
    "    wait_time = random.randint(1, 20)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "111ed56b",
   "metadata": {},
   "source": [
    "# python模块类型\n",
    ">a.标准库\n",
    "\n",
    ">b.开源模块（github等）\n",
    "\n",
    ">c.自定义模块\n",
    "## 标准库\n",
    " - Python的标准库十分庞大，提供的组件涉及范围也十分广泛,允许你访问大量课重用的模块\n",
    " \n",
    " <MARK>标准库=函数+模块<MARK>\n",
    "    \n",
    "### 导入模块\n",
    "\n",
    "- 两种方式\n",
    "\n",
    ">import+模块   \n",
    "\n",
    ">from 模块 import 函数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3711a6d3",
   "metadata": {},
   "source": [
    "## 常用模块介绍\n",
    "### sys模块\n",
    "了解系统"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bb96cb94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'win32'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sys模块访问预属性\n",
    "import sys\n",
    "sys.platform"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2cf7b939",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]\n"
     ]
    }
   ],
   "source": [
    "# 确定python运行版本\n",
    "print(sys.version)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92dc8ee8",
   "metadata": {},
   "source": [
    "### os模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f52a6566",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\coffdaz\\\\jupyter project'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用getcwd得到代码所在文件路径\n",
    "import os\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "497345ae",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "environ{'ALLUSERSPROFILE': 'C:\\\\ProgramData',\n",
       "        'APPDATA': 'C:\\\\Users\\\\coffdaz\\\\AppData\\\\Roaming',\n",
       "        'COMMONPROGRAMFILES': 'C:\\\\Program Files\\\\Common Files',\n",
       "        'COMMONPROGRAMFILES(X86)': 'C:\\\\Program Files (x86)\\\\Common Files',\n",
       "        'COMMONPROGRAMW6432': 'C:\\\\Program Files\\\\Common Files',\n",
       "        'COMPUTERNAME': 'DESKTOP-HTEBPF2',\n",
       "        'COMSPEC': 'C:\\\\windows\\\\system32\\\\cmd.exe',\n",
       "        'DRIVERDATA': 'C:\\\\Windows\\\\System32\\\\Drivers\\\\DriverData',\n",
       "        'HOMEDRIVE': 'C:',\n",
       "        'HOMEPATH': '\\\\Users\\\\coffdaz',\n",
       "        'LOCALAPPDATA': 'C:\\\\Users\\\\coffdaz\\\\AppData\\\\Local',\n",
       "        'LOGONSERVER': '\\\\\\\\DESKTOP-HTEBPF2',\n",
       "        'NUMBER_OF_PROCESSORS': '16',\n",
       "        'ONEDRIVE': 'C:\\\\Users\\\\coffdaz\\\\OneDrive',\n",
       "        'ONEDRIVECONSUMER': 'C:\\\\Users\\\\coffdaz\\\\OneDrive',\n",
       "        'OS': 'Windows_NT',\n",
       "        'PATH': 'D:\\\\conda\\\\anaconda;D:\\\\conda\\\\anaconda\\\\Library\\\\mingw-w64\\\\bin;D:\\\\conda\\\\anaconda\\\\Library\\\\usr\\\\bin;D:\\\\conda\\\\anaconda\\\\Library\\\\bin;D:\\\\conda\\\\anaconda\\\\Scripts;C:\\\\Windows\\\\system32;C:\\\\Windows;C:\\\\Windows\\\\System32\\\\Wbem;C:\\\\Windows\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\;C:\\\\Windows\\\\System32\\\\OpenSSH\\\\;D:\\\\putty\\\\;C:\\\\Users\\\\coffdaz\\\\AppData\\\\Local\\\\Microsoft\\\\WindowsApps;;D:\\\\Pycharm\\\\PyCharm 2021.2.2\\\\bin;',\n",
       "        'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',\n",
       "        'PROCESSOR_ARCHITECTURE': 'AMD64',\n",
       "        'PROCESSOR_IDENTIFIER': 'AMD64 Family 23 Model 96 Stepping 1, AuthenticAMD',\n",
       "        'PROCESSOR_LEVEL': '23',\n",
       "        'PROCESSOR_REVISION': '6001',\n",
       "        'PROGRAMDATA': 'C:\\\\ProgramData',\n",
       "        'PROGRAMFILES': 'C:\\\\Program Files',\n",
       "        'PROGRAMFILES(X86)': 'C:\\\\Program Files (x86)',\n",
       "        'PROGRAMW6432': 'C:\\\\Program Files',\n",
       "        'PROMPT': '$P$G',\n",
       "        'PSMODULEPATH': 'C:\\\\Program Files\\\\WindowsPowerShell\\\\Modules;C:\\\\windows\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\Modules',\n",
       "        'PUBLIC': 'C:\\\\Users\\\\Public',\n",
       "        'PYCHARM': 'D:\\\\Pycharm\\\\PyCharm 2021.2.2\\\\bin;',\n",
       "        'SESSIONNAME': 'Console',\n",
       "        'SYSTEMDRIVE': 'C:',\n",
       "        'SYSTEMROOT': 'C:\\\\windows',\n",
       "        'TEMP': 'C:\\\\Users\\\\coffdaz\\\\AppData\\\\Local\\\\Temp',\n",
       "        'TMP': 'C:\\\\Users\\\\coffdaz\\\\AppData\\\\Local\\\\Temp',\n",
       "        'USERDOMAIN': 'DESKTOP-HTEBPF2',\n",
       "        'USERDOMAIN_ROAMINGPROFILE': 'DESKTOP-HTEBPF2',\n",
       "        'USERNAME': 'coffdaz',\n",
       "        'USERPROFILE': 'C:\\\\Users\\\\coffdaz',\n",
       "        'WINDIR': 'C:\\\\windows',\n",
       "        'ZES_ENABLE_SYSMAN': '1',\n",
       "        'JPY_INTERRUPT_EVENT': '2132',\n",
       "        'IPY_INTERRUPT_EVENT': '2132',\n",
       "        'JPY_PARENT_PID': '2564',\n",
       "        'TERM': 'xterm-color',\n",
       "        'CLICOLOR': '1',\n",
       "        'PAGER': 'cat',\n",
       "        'GIT_PAGER': 'cat',\n",
       "        'MPLBACKEND': 'module://ipykernel.pylab.backend_inline'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问系统的全部环境变量\n",
    "os.environ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ec6ba4f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\conda\\\\anaconda;D:\\\\conda\\\\anaconda\\\\Library\\\\mingw-w64\\\\bin;D:\\\\conda\\\\anaconda\\\\Library\\\\usr\\\\bin;D:\\\\conda\\\\anaconda\\\\Library\\\\bin;D:\\\\conda\\\\anaconda\\\\Scripts;C:\\\\Windows\\\\system32;C:\\\\Windows;C:\\\\Windows\\\\System32\\\\Wbem;C:\\\\Windows\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\;C:\\\\Windows\\\\System32\\\\OpenSSH\\\\;D:\\\\putty\\\\;C:\\\\Users\\\\coffdaz\\\\AppData\\\\Local\\\\Microsoft\\\\WindowsApps;;D:\\\\Pycharm\\\\PyCharm 2021.2.2\\\\bin;'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# getenv访问单独一个环境变量\n",
    "os.getenv('PATH')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "780f9f50",
   "metadata": {},
   "source": [
    "### .datetime模块\n",
    "用于处理时间和日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "845fb169",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2021, 10, 10)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用模块提供今天的时间\n",
    "import datetime\n",
    "datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "80b2e554",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# date.today后调用追加访问属性可单独访问（年，月，日）\n",
    "datetime.date.today().year\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "947e03e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().month\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c266c462",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "92e9eb51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 10, 10, 14, 30, 8, 941948)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime中的 datetime 可以精确到毫秒\n",
    "datetime.datetime.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3d3b935e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-10-10'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# datetime里面的isoformat方法格式化时间，日期会转化成字符串\n",
    "datetime.date.isoformat(datetime.date.today())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d174571",
   "metadata": {},
   "source": [
    "### .time模块\n",
    "- 处理时间可以让时间以自己希望的方式显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2001e76f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=10, tm_mday=10, tm_hour=6, tm_min=35, tm_sec=0, tm_wday=6, tm_yday=283, tm_isdst=0)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入模块\n",
    "import time\n",
    "# 国际标准时间\n",
    "time.gmtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "08c211e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=10, tm_mday=10, tm_hour=14, tm_min=35, tm_sec=33, tm_wday=6, tm_yday=283, tm_isdst=0)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 本地时间\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e523afc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'14:35'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# time里面的 strftime 方法实现24小时制\n",
    "time.strftime(\"%H:%M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "cd65e1ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "#%y 两位数的年份表示（00-99）\n",
    "#%Y 四位数的年份表示（000-9999）\n",
    "#%m 月份（01-12）\n",
    "#%d 月内中的一天（0-31）\n",
    "#%H 24小时制小时数（0-23）\n",
    "#%I 12小时制小时数（01-12）\n",
    "#%M 分钟数（00=59）\n",
    "#%S 秒（00-59）\n",
    "#%a 本地简化星期名称\n",
    "#%A 本地完整星期名称\n",
    "#%b 本地简化的月份名称\n",
    "#%B 本地完整的月份名称\n",
    "#%c 本地相应的日期表示和时间表示\n",
    "#%j 年内的一天（001-366）\n",
    "#%p 本地A.M.或P.M.的等价符\n",
    "#%U 一年中的星期数（00-53）星期天为星期的开始\n",
    "#%w 星期（0-6），星期天为星期的开始\n",
    "#%W 一年中的星期数（00-53）星期一为星期的开始\n",
    "#%x 本地相应的日期表示\n",
    "#%X 本地相应的时间表示\n",
    "#%Z 当前时区的名称\n",
    "#%% %号本身"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5e449f37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-10-10'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime(\"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "443341a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Sunday:PM'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "# 周几？上下午？\n",
    "time.strftime(\"%A:%p\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cae52c44",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
